<?php

namespace app\manage\controller;


use app\common\validate\system\SystemVirtualPackageValid;
use app\manage\loader\ManageServices;
use think\Exception;


/**
 * 虚码套餐管理
 */
class VirtualPackage extends BaseApi
{


    protected function formatWhere($params): array
    {
        $map = [];
        if (!empty($params['keyword'])) {
            $keyword = trim($params['keyword']);
            $map[] = ['title', 'like', '%' . $keyword . '%'];
        }
        return $map;
    }

    /**
     * 虚码套餐列表
     * @return void
     */
    public function page(): void
    {
        try {
            $getData = input('get.');
            $option = $this->formatOption($getData);
            $option['where'] = $this->formatWhere($getData);
            $result = ManageServices::VirtualPackage()->packageList($option);
            api_result($result);
        } catch (Exception $e) {
            api_result($e);
        }
    }
    // 添加修改

    /**
     * 添加虚码套餐
     * @return void
     */
    public function save(): void
    {
        try {
            $postData = input('post.');
            $scene = empty($postData['id']) ? 'add' : 'edit';
            validate(SystemVirtualPackageValid::class)->scene($scene)->check($postData);
            $result = ManageServices::VirtualPackage()->packageSave($postData, $scene);

            api_result(API_SUCCESS, ($scene === 'add' ? '添加' : '修改') . '成功', ['id' => $result]);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 设置套餐状态
     * @return void
     */
    public function edit_status(): void
    {
        try {
            $postData = input('post.');
            validate(SystemVirtualPackageValid::class)->scene('editStatus')->check($postData);
            ManageServices::VirtualPackage()->editStatus($postData['id'], $postData['status']);
            api_result(API_SUCCESS, '设置成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 虚码套餐详情
     * @return void
     */
    public function info(): void
    {
        $getData = input('get.');
        try {
            validate(SystemVirtualPackageValid::class)->scene('info')->check($getData);
            $result = ManageServices::VirtualPackage()->info($getData['id']);
            api_result(API_SUCCESS,'请求成功',$result);
        } catch (Exception $e) {
            api_result($e);
        }
    }
}
